package com.mycar2w.cloud.web.controller;

import com.mycar2w.cloud.service.exception.UnLoadExceptiopn;
import com.mycar2w.cloud.service.repository.TUserRepository;
import com.umuw.cloud.core.feature.RedisManager;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * created time 2017/4/3
 *
 * @author ljc
 * @version V1.0
 */
public class MycarBaseConTroller {
    @Autowired
    protected HttpServletRequest httpServletRequest;
    @Autowired
    private TUserRepository tUserRepository;
    @Autowired
    private RedisManager redisManager;
    @Autowired
    protected HttpSession httpSession;
    public void setSessionToken(String phone){
        String token= UUID.randomUUID().toString().replace("-","");
        redisManager.set("mycar2w."+token,phone.trim());
        httpSession.setAttribute("myphone",token);
    }
    public String getToken(){
        String token= (String) httpSession.getAttribute("myphone");
        return token;
    }
    public String getMypPhone(){
        String phone;
        String token= (String) httpSession.getAttribute("myphone");
        if(!StringUtils.isNotBlank(redisManager.get("mycar2w."+token))){
            throw new UnLoadExceptiopn();
        }else {
            phone=redisManager.get("mycar2w."+token);
            return phone;
        }
    }
    public void cleanLogin(){
        httpSession.invalidate();
        redisManager.delKey("mycar2w"+getToken(),0);
    }
    public void getMaster(){
        String phone=getMypPhone();
        if(!tUserRepository.findByPhone(phone).getRole().equals("0")){
            throw new UnLoadExceptiopn();
        }
    }
}
